package com.wang.designpattern.singleton.type5;

public class SingletonTest05 {

    public static void main(String[] args) {
        System.out.println("静态内部类（线程安全，懒加载）");
        Singleton singleton1 = Singleton.getInstance();
        Singleton singleton2 = Singleton.getInstance();
        System.out.println(singleton1 == singleton2);
        System.out.println("singleton1.hashCode() == " + singleton1.hashCode());
        System.out.println("singleton2.hashCode() == " + singleton2.hashCode());
    }

}

class Singleton {
    // 1、私有化构造函数，外部不能new
    private Singleton() {}

    // 2、写一个静态内部类，该类中有一个静态属性Singleton
    private static class SingletonInstance{
        private static final Singleton INSTANCE = new Singleton();

    }

    // 4、提供一个公有的静态方法，直接返回SingletonInstance.INSTANCE
    public static Singleton getInstance() {
        return SingletonInstance.INSTANCE;
    }
}
